#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <stdint.h>
#include <pthread.h>
#include <thread>

#include "ArmorPlate.h"     //非系统库
#include "camera_class.h"   //非系统库
#include "Serial.h"      //非系统库
#include "solveangle.hpp"

using namespace cv;
using namespace std;

extern float S_yaw = 0.0;
extern float S_pitch = 0.0;

extern float R_yaw = 0.0;
extern float R_pitch = 0.0;

extern bool pre_mode = false;   //是否开启预测模式

extern pthread_t send_tid, recv_tid, monitor_tid;

int fd; // 全局文件描述符，供线程共享

volatile int keep_running = 1; // 控制线程退出的变量

int main()
{
    fd = open_uart();   // judge whether the uart is opened
    if(fd == -1)
        return -1;      // exit and return -1

    camera cam;
    Mat image;
    cam.start_cam();    //开启摄像头
    
    while (true)
    {
        cam.get_pic(&image);  //获取画面
        flip(image, image, 1);  //图像翻转
        if(image.empty())
        {
            cout << "Find no image!" << endl;
            break;
        }
        AngleSolve foo(image);
        // armor.autoArmorPlate();     //调用找装甲板模块
        S_yaw = float(foo.yaw_get());
        S_pitch = float(foo.pitch_get());
        Mat img = foo.img_get();
        imshow("result", img);
        if('q' == waitKey(1))
        {
            cam.close_cam();
            break;
        }
    }
    
    // 等待线程结束
    pthread_join(send_tid, NULL);
    pthread_join(recv_tid, NULL);
    pthread_join(monitor_tid, NULL);

    while('q' != getchar())
        continue;
    // 退出线程
    keep_running = 0;
    close(fd);

    return 0;
}